package com.tencent.atk.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.*;
import android.widget.TextView;

import com.tencent.atk.Constants;
import com.tencent.atk.R;
import com.tencent.atk.core.MemoryManager;
import com.tencent.atk.core.TrafficManager;
import com.tencent.atk.util.ToastUtil;
import com.tencent.atk.view.FloatView;

/**
 * Created with IntelliJ IDEA.
 * User: liurenhui
 * Date: 13-6-20
 * Time: PM12:27
 * To change this template use File | Settings | File Templates.
 */
public class MainService extends Service {
    private static final int DELAY_TIME = 1000;
    private WindowManager windowManager;
    private WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
    private FloatView floatView;
    private float mTouchStartX;
    private float mTouchStartY;
    private float x;
    private float y;
    private Handler handler = new Handler();
    private boolean isServiceStop = false;
    private TextView trafficAll;
    private TextView trafficAdd;
    private long initTraffic = -1;
    private int pid;
    private int uid;

    @Override
    public void onDestroy() {
        super.onDestroy();
        windowManager.removeView(floatView);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        createFloatingWindow();
        isServiceStop = false;
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    	pid = intent.getIntExtra(Constants.PID, -1);
        uid = intent.getIntExtra(Constants.UID, -1);
        if (uid != -1)
        	handler.postDelayed(task, DELAY_TIME);
        else
        	ToastUtil.longToast(this, "请打开要测试的程序");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onStart(Intent intent, int startId) {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private Runnable task = new Runnable() {
        @Override
        public void run() {
            if (!isServiceStop) {
                updateInformation();
                handler.postDelayed(this, DELAY_TIME);
            } else {
                stopSelf();
            }
        }
    };

    private void updateInformation() {
    	long memory = MemoryManager.getPidMemorySize(this, pid);
    	floatView.updateMemory(memory);
         if (initTraffic == -1) {
        	 long traffic = TrafficManager.getAllTraffic(uid);
        	 initTraffic = traffic;
        	 Log.d(Constants.TAG, "traffic:" + initTraffic);
         } else {
        	 long tmpTraffic = TrafficManager.getAllTraffic(uid);
        	 long incTraffic = tmpTraffic - initTraffic;
        	 floatView.updateTraffic(incTraffic);
        	 Log.d(Constants.TAG, "traffic:" + incTraffic);
         }
    }

    private void createFloatingWindow() {
        floatView = new FloatView(this);
        windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        wmParams.type = 2002;
        wmParams.flags |= 8;
        wmParams.gravity = Gravity.LEFT | Gravity.TOP;
        wmParams.x = 0;
        wmParams.y = 0;
        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.format = 1;
        windowManager.addView(floatView, wmParams);
        floatView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                x = event.getRawX();
                y = event.getRawY() - 25;
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        // state = MotionEvent.ACTION_DOWN;
                        mTouchStartX = event.getX();
                        mTouchStartY = event.getY();
                        Log.d("startP", "startX" + mTouchStartX + "====startY" + mTouchStartY);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        // state = MotionEvent.ACTION_MOVE;
                        updateViewPosition();
                        break;

                    case MotionEvent.ACTION_UP:
                        // state = MotionEvent.ACTION_UP;
                        updateViewPosition();
                        mTouchStartX = mTouchStartY = 0;
                        break;
                }
                return true;
            }
        });
    }

    private void updateViewPosition() {
        wmParams.x = (int) (x - mTouchStartX);
        wmParams.y = (int) (y - mTouchStartY);
        windowManager.updateViewLayout(floatView, wmParams);
    }
}
